package com.wash.shoes.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wash.shoes.entity.ShoeRackGridPo;
import com.wash.shoes.entity.ShoeRackPo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.mapper.ShoeRackMapper;
import com.wash.shoes.page.PageFactory;
import com.wash.shoes.service.ShoeRackService;
import com.wash.shoes.util.HeaderUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 店铺的鞋架 服务实现类
 * </p>
 *
 * @author ljg
 * @since 2024-08-27
 */
@Log4j2
@Service
public class ShoeRackServiceImpl extends ServiceImpl<ShoeRackMapper, ShoeRackPo> implements ShoeRackService {

    @Override
    public Page<ShoeRackPo> pageList(ShoeRackPo shoeRackPo) {
        Page page = PageFactory.defaultPage();
        List<ShoeRackPo> dataList = baseMapper.pageList(page, shoeRackPo, HeaderUtil.getShopIdStr());
        page.setRecords(dataList);
        return page;
    }

    @Override
    public List<ShoeRackPo> getByCodeId(String code, String id) {
        return this.baseMapper.getByCodeId(code, id);
    }

    @Override
    public Integer updateUseNumInteger(String id) {
        return this.baseMapper.updateUseNumInteger(id);
    }

    @Override
    public Integer updateUseNum(List<ShoeRackGridPo> shoeRackGridPoList) {
        List<String> rackIds = shoeRackGridPoList.stream()
                .map(ShoeRackGridPo::getShoeRackId)
                .distinct()
                .collect(Collectors.toList());
        for (String rackId : rackIds) {
            this.baseMapper.updateUseNumInteger(rackId);
        }
        return 1;
    }

    @Override
    public List<ShoeRackPo> getAllRackList() {
        return this.baseMapper.getAllRackList();
    }

    @Override
    public List<ShoeRackPo> userShoeRackList() {
        String ids = HeaderUtil.getShopIdStr();
        log.info("用户店铺 ids is {}", ids);
        if (StrUtil.isBlank(ids)) {
            throw new WashException("用户店铺信息不存在");
        }


        return this.baseMapper.userShoeRackList(ids);
    }


}
